#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this ->init();
}

MainWindow::~MainWindow()
{
    if(BtnRes!=NULL)
    {
        delete BtnRes;
    }
    delete ui;
}
void MainWindow:: init(){

    this ->setFixedSize(800,200);

    leftendit = new QLineEdit("0",this);
    ringhtendit = new QLineEdit("0",this);
    BtnRes = new QPushButton("=");
    BtnRes->show();

    cbx = new QComboBox(this);
    QStringList listOperator={"+","-","*","÷","%"};
    cbx->addItems(listOperator);
    Labelresult = new QLabel("结果",this);
    LabelMeassage = new QLabel("提示",this);

    leftendit ->setGeometry(20,20,190,70);
    cbx ->setGeometry(220,20,70,70);
    ringhtendit ->setGeometry(300,20,190,70);
    BtnRes->setParent(this);
    BtnRes ->setGeometry(500,20,70,70);
    Labelresult ->setGeometry(580,20,200,70);
    LabelMeassage ->setGeometry(20,110,760,70);
    LabelMeassage->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    string style=R"(
                     *{
                     font-size: 20px;
                     font-family:'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
                     }

                     QLabel
                     {
                     background-color: rgb(255, 85, 0);
                     color:#fff
                     }


                     QPushButton{
                     background-color:green;
                     color:#fff;
                     }

                     QLineEdit
                     {
                     background-color:deeppink;
                     color:#fff
                     }

                     )";
    qApp -> setStyleSheet(style.data());
     qDebug()<<"1111111111";

    connect(BtnRes,SIGNAL(clicked()),this,SLOT(doRes()));
     qDebug()<<"3333333333333";

}
void MainWindow:: doRes(){
   qDebug()<<"222222222222";
    Labelresult->setText("          ");
    LabelMeassage->setText("            ");
    QString textLeft = leftendit->text();
    QString textringht = ringhtendit->text();
    if(textLeft.length()==0){
        LabelMeassage->setText("请输入操作数1");
        ringhtendit->setFocus();
        return;
    }
    if(textringht.isEmpty()){
        LabelMeassage->setText("请输入操作数2");
        ringhtendit->setFocus();
        return;
    }
    double left,right;
    bool ok;
    left = textLeft.toDouble(&ok);
    if(!ok){
        LabelMeassage->setText("操作数1不是重数字");
        return;
    }
    right = textringht.toDouble(&ok);
    if(!ok){
        LabelMeassage->setText("操作数2不是纯数字");
    }
    double res;
    switch (cbx->currentIndex()) {
    case 0:
        res = left+right;
        Labelresult->setText(QString::number(res));
        return;

    case 1:
        res = left-right;
        Labelresult->setText(QString::number(res));
        return;
    case 2:
        res = left*right;
        Labelresult->setText(QString::number(res));
        return;
    case 3:
        if(right == 0){
            LabelMeassage->setText("除数不能为0");
            return;
        }
        res = left/right;

        Labelresult->setText(QString::number(res));
        return;


    case 4:
        int one,two;
        one = (int)(left);
        leftendit->setText(QString::number(one));
        two = (int)(right);
        ringhtendit->setText(QString::number(two));
        if(two == 0){
            LabelMeassage->setText("取模运算时操作数2不能为0");
            return;
        }
       int res = one%two;
        Labelresult->setText(QString::number(res));
        return;



    }



}
